<?php

class Application{

    public $db;
    public $session;
    public $lang;

    //Uses to make right highlighting;
    public $increment = 0;

    function __construct(){
	ob_start();
            if(requestlog===true){
                $this->write($this->get_request_data(), _ROOT_.'requestlog');
            }
	}

private function load($instance,$dir,$data=null){
           //Check File For Real Existanse.
            if(file_exists(_APP_.$dir.$instance)){

            //Make $data array looks like ordenary variables.
            if(!is_null($data)){
                foreach($data as $index=>$value){
                $$index=$value;
                }
            }

            global $application,$G,$P,$S,$O;
            if(debug==true){
		echo "<b> - ".$dir.$instance." was loaded - </b>";
            }
            require(_APP_.$dir.$instance);

            }else{
                die('Loading... Error: '._APP_.$dir.$instance.' not exist');
            }
}

        //Main load will load up controllers.
	public function controller($instance,$data=null){
	$this->load($instance.".php","controllers/",$data);
        }

        //Main load will load up views.
	public function view($instance,$data=null){
	$this->load($instance.".tpl","views/",$data);
        }

	public function redirect($route){
        ob_end_flush();
	header("Location: {$route}");
	exit();
	}

        public function language($lang){
            $this->lang = $lang;
            $this->session->set('selected_language',$lang);
            @include_once(_APP_.'localizations/'.$lang.'/general.php');
        }

	public function package($pack){
            $this->lang = $this->session->get('selected_language');
            $lang = $this->lang;
            @include_once(_APP_.'localizations/'.$lang.'/'.$pack.'.php');
            if(debug==true){
                echo "<span style='color: yellow;'>Loaded: [{$lang}] => [{$pack}]</span>";
		}
        }

        public function error($text){
            die( "<br/><br/><hr/><center><div><h1>{$text}</h1></div></center><hr/>" );
            exit();
        }

        public function write($data, $path){
            file_put_contents($path, file_get_contents($path).$data);
        }

        private function get_request_data(){
            return $_SERVER['REMOTE_ADDR'].' GET '.$_SERVER['PHP_SELF'].' WITH '.$_SERVER['QUERY_STRING'].' FROM '.$_SERVER['HTTP_REFERER'].' AT '.time().PHP_EOL;
        }
}

?>